package com.itheima.service.system.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.UserDao;
import com.itheima.domain.system.User;
import com.itheima.factory.MapperFactory;
import com.itheima.service.system.UserService;
import com.itheima.utils.MD5Util;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class UserServiceImpl  implements UserService {

    @Override
    public void save(User user) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            UserDao userDao=MapperFactory.getMapper(sqlSession,UserDao.class);
            String id= UUID.randomUUID().toString();
            user.setId(id);

            user.setPassword(MD5Util.md5(user.getPassword()));
            userDao.save(user);
            TransactionUtil.commit(sqlSession);
        }catch(Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void delete(User user) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            UserDao userDao=MapperFactory.getMapper(sqlSession,UserDao.class);
            userDao.delete(user);
            TransactionUtil.commit(sqlSession);
        }catch(Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(User user) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            UserDao userDao=MapperFactory.getMapper(sqlSession,UserDao.class);
            userDao.update(user);
            TransactionUtil.commit(sqlSession);
        }catch(Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public User findById(String id) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            UserDao userDao=MapperFactory.getMapper(sqlSession,UserDao.class);
            return userDao.findById(id);
        }catch(Exception e) {
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<User> findAll() {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            UserDao userDao=MapperFactory.getMapper(sqlSession,UserDao.class);
            return userDao.findAll();
        }catch(Exception e) {
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageInfo<User> findAll(int page, int size) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            UserDao userDao=MapperFactory.getMapper(sqlSession,UserDao.class);
            PageHelper.startPage(page,size);
            List<User>all=userDao.findAll();
            PageInfo pageInfo=new PageInfo(all);
            return pageInfo;
        }catch(Exception e) {
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
